<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Member_index_module extends CI_Module {

    private   $data ;
    private   $sid ;
    public function __construct()
    {
        parent::__construct();
        $this->sid = $GLOBALS['sid'];
        $this->data = $GLOBALS['config'];
        $this->load->model('member.User_model');
        $this->load->model('order.orderinfo_model');
        
        $this->load->model('admin.Upload_model');
        $this->load->model('comment.comment_model');
    }

    public function center_head(){

        $this->load->view(TEMPLATE_NAME.'/page_center_header');
    }

    public function center_left($params){
        $data  =array();
        $data['left']=$params['nav']['left'];
        $data['order']=@$params['nav']['order'];
        $this->load->view(TEMPLATE_NAME.'/page_center_left',$data);
    }

    /**
     * 会员头部信息
     * @param array $seo
     */
    public function head($seo=array()){
        //seo信息
        $data['seo'] = $seo;
        $this->load->view(TEMPLATE_NAME.'/page_header',$data);
    }

    /**
     * 个人中心
     */

    public function center(){

        $user_id = $this->session->userdata('user_id');
        if(!$user_id)
        {
            redirect(site_url('/member/index/login'));
        }
        $this->data = $this->User_model->_user_info($user_id);
        $this->data['user_id'] = $user_id;
        $this->data['bread'] = '我的首页';
        $this->data['params'] = array(
            'seo'=>array(
                'title'=>'会员中心',
                'keywords'=>'会员中心',
                'desc'=>'会员中心'
            ),
            'nav'=>array('current'=>'center'),
        );
        $this->data['extra'] = array(array(
            'nav'=>array('left'=>'center','current'=>'center')
        ));
        $this->load->view(TEMPLATE_NAME.'/view_center',$this->data);
    }

    /**
     * 个人中心
     */

    public function comment(){

        $user_id = $this->session->userdata('user_id');
        if(!$user_id)
        {
            redirect(site_url('/member/index/login'));
        }

        $module = $_GET['module'];
        if(!$module){
            $module ='line';
        }

        $page_config['perpage']=10;   //每页条数
        $page_config['part']=3;//当前页前后链接数量
        $page_config['url'] = "member/index/comment";//url
        $page_config['param'] = "?module=$module";//url

        $page_config['nowindex'] = $this->uri->segment(4)>0 ? $this->uri->segment(4):1;//当前页
        $total= $this->comment_model->get_my_comments_count($module);
        $page_config['total'] = $total;

        $this->load->library('mypageclass');
        $this->mypageclass->initialize($page_config);


        $this->data['list'] = $this->comment_model->get_my_comments($module, $page_config['perpage'], $page_config['nowindex']);

        $this->data['user_id'] = $user_id;
        $this->data['bread'] = '我的点评';
        $this->data['params'] = array(
            'seo'=>array(
                'title'=>'会员中心',
                'keywords'=>'会员中心',
                'desc'=>'会员中心'
            ),
            'nav'=>array('current'=>'comment'),
        );
        $this->data['extra'] = array(array(
            'nav'=>array('left'=>'comment','current'=>'comment')
        ));
        $this->load->view(TEMPLATE_NAME.'/view_comment',$this->data);
    }

    /**
     * 修改手机号
     */
    public function modify_mobile(){

        if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
        {
            $data = $this->input->get_post('Form');
            if(strtoupper($data['code'])==strtoupper($this->session->userdata('register_code')))
            {
                $where = array('user_id'=>$this->session->userdata('user_id'));
                $base_data = array('mobile'=>$data['mobile']) ;
                $res = $this->User_model->_edit_member($base_data,$where) ;
                if($res)
                {
                    $this->session->unset_userdata('username');
                    $this->session->unset_userdata('user_id');
                    $this->session->unset_userdata('is_login');
                    $this->success('操作成功',site_url('member/index/login'),true);
                }
                else
                {
                    $this->error('操作失败','',true);
                }
            }
            else
            {
                $this->error('验证码错误','',true);
            }
        }
        $user_id = $this->session->userdata('user_id');
        if(!$user_id)
        {
            redirect(site_url('/member/index/login'));
        }
        $this->data = $this->User_model->_user_info($user_id);
        $this->data['user_id'] = $user_id;
        $this->data['mobile_verify'] = site_url('/member/index/verify_gateway/?action=mobile_ver') ;
        $this->data['form_url']=  site_url('/member/index/modify_mobile');
        $this->data['bread']=  '修改手机号';
        $this->data['extra'] = array(array(
            'nav'=>array('left'=>'info')
        ));
        $this->data['params'] = array(
            'seo'=>array(
                'title'=>'会员中心',
                'keywords'=>'会员中心',
                'desc'=>'会员中心'
            ),
            'nav'=>array('current'=>'center'),
        );
        $this->load->view(TEMPLATE_NAME.'/view_mobile',$this->data);
    }


    /**
     * 修改邮箱
     */

    public function modify_email(){

        if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
        {
            $action = $this->input->get_post('action');
            $data = $this->input->get_post('Form');

                if(strtoupper($data['code']) == strtoupper($this->session->userdata('register_code')))
                {
                    $where = array('user_id'=>$this->session->userdata('user_id'));
                    $base_data = array('email'=>$data['email']) ;
                    $res = $this->User_model->_edit_member($base_data,$where) ;
                    if($res)
                    {
                        $this->session->unset_userdata('username');
                        $this->session->unset_userdata('user_id');
                        $this->session->unset_userdata('is_login');
                        $this->success('操作成功',site_url('member/index/login'),true);
                    }
                    else
                    {
                        $this->error('操作失败','',true);
                    }
                }
                else
                {
                    $this->error('验证码错误','',true);
                }
        }
        $user_id = $this->session->userdata('user_id');
        if(!$user_id)
        {
            redirect(site_url('/member/index/login'));
        }
        $this->data = $this->User_model->_user_info($user_id);
        $this->data['user_id'] = $user_id;
        $this->data['email_verify'] = site_url('/member/index/verify_gateway/?action=email_ver') ;
        $this->data['form_url']=  site_url('/member/index/modify_email');
        $this->data['bread']=  '修改邮箱';
        $this->data['extra'] = array(array(
            'nav'=>array('left'=>'info')
        ));
        $this->data['params'] = array(
            'seo'=>array(
                'title'=>'会员中心',
                'keywords'=>'会员中心',
                'desc'=>'会员中心'
            ),
            'nav'=>array('current'=>'center'),
        );
        $this->load->view(TEMPLATE_NAME.'/view_email',$this->data);
    }




    /**
     * 个人资料
     */

    public function info(){


        if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
        {
            $failure_url = site_url('/member/index/info');
            $user_id = $this->input->get_post('user_id');
            $where = array('user_id'=>intval($user_id));
            $base_data = $this->input->get_post('Form');
            $expand_data = $this->input->get_post('Expand');
            if(!empty($base_data['password']))
            {
                $ran_str = $this->User_model->_rand_6_char();
                $base_data['password'] = md5(md5($base_data['password'].$ran_str));
                $base_data['mix'] = $ran_str ;
            }
            $base_data = array_filter($base_data);
            $expand_data = array_filter($expand_data);
            if(!empty($base_data))
            {
                $res = $this->User_model->_edit_member($base_data,$where) ;
            }
            else
            {
                $res = TRUE ;
            }
            if(!empty($expand_data))
            {
                $res1 = $this->User_model->_edit_member_expand($expand_data,$where) ;
            }
            else
            {
                $res1 = TRUE ;
            }
            if($expand_data['id_type']==1){
                if(!isCreditNo($expand_data['id_num'])){
                    $this->error('身份证号码不正确！',$failure_url,true);
                }
            }
            if(DEBUG){
                print_r($base_data);
                print_r($expand_data);
                print_r($where);
                print_r($this->db->queries);
                exit;
            }


            if($res&&$res1)
            {
                $this->success('操作成功',$failure_url,true);
            }
            else
            {
                $this->error('操作失败',$failure_url,true);
            }
        }

        $user_id = $this->session->userdata('user_id');
        if(!$user_id)
        {
            redirect(site_url('/member/index/login'));
        }
        $this->data = $this->User_model->_user_info($user_id);
        $this->data['user_id'] = $user_id;
        $this->data['form_url']=  site_url('/member/index/info');
        $this->data['check_username_uri'] = site_url('/member/index/verify_gateway/?user_id='.$user_id.'&action=username_ver') ;
        $this->data['bread'] = '个人中心' ;
        $this->data['extra'] = array(array(
            'nav'=>array('left'=>'info')
        ));
        $this->data['params'] = array(
            'seo'=>array(
                'title'=>'我的信息',
                'keywords'=>'会员中心',
                'desc'=>'会员中心'
            ),
            'nav'=>array('current'=>'center'),
        );
        if(DEBUG){
            print_r($this->data);
        }
        $this->load->view(TEMPLATE_NAME.'/view_info',$this->data);
    }


    /**
     * 修改密码
     */

    public function modify_password(){

        if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
        {
            $data = $this->input->get_post('Form');
            $res = $this->User_model->_modify_password($data['old'],$data['password']);
            if($res['status']=='1')
            {
                $this->SUCCESS('修改密码成功',site_url('member/index/login'),true);
            }
            if($res['status']=='0')
            {
                $this->error('原密码不正确，修改失败！','',true);
            }
        }

        if(!$this->session->userdata('user_id'))
        {
            redirect(site_url('/member/index/login'));
        }
        $this->data['form_url'] =  site_url('/member/index/modify_password');
        $this->data['bread'] = '修改密码' ;
        $this->data['extra'] = array(array(
            'nav'=>array('left'=>'pwd', 'left1'=>'pwd')
        ));
        $this->data['params'] = array(
            'seo'=>array(
                'title'=>'会员中心',
                'keywords'=>'会员中心',
                'desc'=>'会员中心'
            ),
            'nav'=>array('current'=>'center'),
        );
        $this->load->view(TEMPLATE_NAME.'/view_modify',$this->data);
    }


    /**
     * AJAX 登陆状态验证 / 注销登陆
     */

    public function login_verify()
    {
        $action = $this->input->get_post('action');
        if(isset($action) && $action == 'login')
        {
            $session = $this->session->all_userdata();
            if ( ! empty($session['user_id'])&&$session['is_login'] == '1')
            {
                if ( ! empty($session['email']))
                {
                   $data = $session['email'] ;
                }
                if ( ! empty($session['mobile']))
                {
                    $data = $session['mobile'] ;
                }
                if ( ! empty($session['username']))
                {
                    $data = $session['username'] ;
                }
                return json_encode(array('status'=>'1', 'action'=>'login', 'data'=>sub_str($data,0,3))) ;
            }
        }

        if($action=='unset')
        {
            $this->session->unset_userdata('username');
            $this->session->unset_userdata('user_id');
            $this->session->unset_userdata('is_login');
            return json_encode(array('status'=>'1','action'=>'unset','data'=>'')) ;
        }
        return json_encode(array('status'=>'0','action'=>'','data'=>'')) ;
    }

    /**
     * 找回密码
     */

    public function password()
    {
        if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
        {
            $data = $this->input->get_post('Form');
            if (strtoupper($data['register_code']) == strtoupper($this->session->userdata('register_code')))
            {
                if ( ! empty($data['email']))
                {
                    $account = $data['email'] ;
                }
                if ( ! empty($data['mobile']))
                {
                    $account = $data['mobile'] ;
                }

                $res = $this->User_model->_unset_password($account,$data['password'] );
                if ($res['status'] == '1')
                {
                    $this->SUCCESS('找回密码成功',site_url('member/index/login'),true);
                }
                if ($res['status'] == '0')
                {
                    $this->error('找回密码失败', '', true);
                }
            }
            else
            {
                $this->error('验证码错误', '', true);
            }
        }

        $this->data['phone'] = $this->data['site_service_phone'];
        $this->data['mobile_verify'] =  site_url('/member/index/mobile_exist/') ;
        $this->data['email_verify'] =  site_url('/member/index/email_exist/') ;
        $this->data['form_url']= site_url('member/index/password');
        $this->data['title']= '找回密码';
        $this->load->view(TEMPLATE_NAME.'/view_password',$this->data);
    }

    /**
     * 注册
     */

    public function register()
    {
        if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
        {
            $data = $this->input->get_post('Form');

            if (strtoupper($data['register_code']) == strtoupper($this->session->userdata('register_code')))
            {
                if ( ! empty($data['email']))
                    $account = $data['email'] ;

                if ( ! empty($data['mobile']))
                    $account = $data['mobile'] ;

                $res = $this->User_model->_register_verify($data['username'], $account, $data['password'] );
                if ($res['status'] == '1')
                    $this->SUCCESS('注册成功', site_url('member/index/login'), true);

                if ($res['status'] == '0')
                    $this->error('注册失败', '', true);
            }
            else
            {
                $this->error('验证码错误', '', true);
            }
        }
        $this->data['phone'] = $this->data['site_service_phone'];
        $this->data['sitelogo'] = $this->data['sitelogo'];
        $this->data['captcha_verify'] = site_url('/member/index/captcha_verify') ;
        $this->data['username_verify'] =  site_url('/member/index/verify_gateway/?action=username_ver') ;
        $this->data['mobile_verify'] =  site_url('/member/index/verify_gateway/?action=mobile_ver') ;
        $this->data['email_verify'] =  site_url('/member/index/verify_gateway/?action=email_ver') ;
        $this->data['form_url']= site_url('member/index/register');
        $this->data['title']= '注册会员';
        $this->data['params'] = array(
            'seo'=>array(
                'title'=>"注册",
                'keywords'=>"注册",
                'desc'=>"注册"
            ),
        );
        $this->load->view(TEMPLATE_NAME.'/view_register', $this->data);
    }


    /**
     * 登陆
     */

    public function login()
    {
        if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
        {
            $data = $this->input->get_post('Form');
            $auto = '' ;
            if (isset($data['auto']))  $auto = 'on' ;
            $res = $this->User_model->_login_verify($data['account'],$data['password'],$auto);
            if ($res['status']=='1')
            {
                $url = site_url();
                if(!empty($_POST['referer']))
                    $url = $_POST['referer'];

                $this->success($res['info'],$url,true);
            }
            else
            {
                $this->error($res['info'],site_url('/member/index/login'),true);
            }
        }
        $this->data['phone'] = $this->data['site_service_phone'];
        $this->data['sitelogo'] = $this->data['sitelogo'];
        $this->data['form_url']= site_url('member/index/login');
        $this->data['params'] = array(
            'seo'=>array(
                'title'=>"登录",
                'keywords'=>"登录",
                'desc'=>"登录"
            ),
        );
        $this->load->view(TEMPLATE_NAME.'/view_login',$this->data);

    }



    /**
     * 发送验证码
     */

    public function send_register_code()
    {
            $rand = $this->User_model->_rand_6_char();
            $template = $this->data['verification_sms_template'];
            $template = str_replace(array('{$code}'),$rand,$template) ;
            $this->session->sess_expiration = '60';
            $this->session->set_userdata('register_code',$rand);
            $account = $this->input->get_post('account');
            $res = false ;
            if($this->User_model->_check_is_mobile($account))
            {
                $res =  $this->User_model->_send_sms($account,$template);
            }

            if($this->User_model->_check_is_email($account))
            {

                $res =  $this->User_model->_send_email($account,'网站验证码',$template);
            }

            if($res)
            {
                return json_encode(array('status'=>'1'));
            }
            else
            {
                return json_encode(array('status'=>'0'));
            }
    }

    /**
     * 图片验证码验证
     */

    public function captcha_verify()
    {
        $arg_get = $this->input->get();
        $this->load->library('mycaptchaclass');
        if(!$this->mycaptchaclass->check($arg_get['Form']['captcha']))
        {
            $this->ajaxReturn('验证码错误');
        }
        else
        {
            $this->ajaxReturn(true);
        }
    }



    /**
     * 用户名验证
     */

    public function username_ver($username)
    {
        $return = $this->User_model->_username_ver($username);
        if($return['status']=='1')
        {
            return TRUE ;
        }
        else
        {
            return FALSE ;
        }
    }

    /**
     * 邮箱验证重复
     */

    public function email_ver($email)
    {
        $return = $this->User_model->_email_ver($email);
        $this->ajaxReturn($return['info']);
    }

    /**
     * 手机号验证重复
     */

    public function mobile_ver($mobile)
    {
        $return = $this->User_model->_mobile_ver($mobile,$userid='');
        $this->ajaxReturn($return['info']);

    }

    /**
     * 邮箱验证存在
     */

    public function email_exist()
    {
        $arg_get = $this->input->get();
        $email = $arg_get['Form']['email'];
        $return = $this->User_model->_email_exist($email);
        $this->ajaxReturn($return['info']);
    }

    /**
     * 手机号验证存在
     */

    public function mobile_exist()
    {
        $arg_get = $this->input->get();
        $mobile = $arg_get['Form']['mobile'];
        $return = $this->User_model->_mobile_exist($mobile);
        $this->ajaxReturn($return['info']);
    }



    /**
     * 注册/登陆 验证入口
     * @param   POST 表单数据
     */


    public function verify_gateway()
    {

        $action = $this->input->get_post('action');
        $user_id = $this->input->get_post('user_id');
        $arg_get = $this->input->get();
        extract($_GET);

        switch($action){


            case "email_ver":

                $return = $this->User_model->_email_ver($arg_get['Form']['email'],$user_id);
                $this->ajaxReturn($return['info']);

                break ;

            case "username_ver":

                $return = $this->User_model->_username_ver($arg_get['Form']['username'],$user_id);
                $this->ajaxReturn($return['info']);

                break ;
            case "mobile_ver":

                $return = $this->User_model->_mobile_ver($arg_get['Form']['mobile'],$user_id);
                $this->ajaxReturn($return['info']);

                break ;

        }
    }


    public function upload_avatar()
    {
        $user_id = $this->session->userdata('user_id');
        $config = $this->Upload_model->upload_config('avatar');
        $this->load->library('upload',$config);
        $absolute_path = str_replace('\\', '/', getcwd()).'/';
        if ( ! $this->upload->do_upload('file'))
        {
            $error = array('error' => $this->upload->display_errors());
            $this->error($error['error'],'',true);
        }else
        {
            $data = array('upload_data' => $this->upload->data());
            $message = str_replace($absolute_path, '', $data['upload_data']['full_path']);
            $file_ext = $data['upload_data']['file_ext'] ;
            $dir_url = '/uploads/'.$GLOBALS['sid'].'/avatar/' ;
            $des_url = $dir_url.$user_id.$file_ext ;
            $des = getcwd().$des_url;
            if(copy($message,$des))
            {
                if(unlink($message))
                {
                    $new_url = substr($des_url,1);
                    $where = array('user_id'=>$user_id);
                    $array['avatar'] = $new_url ;
                    $res = $this->User_model->_edit_member_expand($array,$where) ;
                }
            }

            
           
            $this->success(URLPIC.$new_url.'?_='.time(),'',true);
        }

    }
}
